package com.huatai.manage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.common.util.BeanUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.manage.entity.TbCodeMainEntity;
import com.huatai.manage.entity.TbCodeManageEntity;
import com.huatai.manage.entity.TbFirmManageEntity;
import com.huatai.manage.mapper.TbCodeMainMapper;
import com.huatai.manage.mapper.TbCodeManageMapper;
import com.huatai.manage.query.PageQuery;
import com.huatai.manage.service.TbCodeManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.manage.utils.ComUtil;
import com.huatai.manage.vo.PageVo;
import com.huatai.manage.vo.TbCodeManageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 编码管理（字典）; 服务实现类
 * </p>
 *
 * @author luoxc
 * @since 2023-07-05
 */
@Service
public class TbCodeManageServiceImpl extends ServiceImpl<TbCodeManageMapper, TbCodeManageEntity> implements TbCodeManageService {

	@Autowired
	private TbCodeManageMapper mapper;

	@Override
	public PageVo pageList(PageQuery<TbCodeManageEntity> pageQuery) {
		Page<TbCodeManageEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		TbCodeManageEntity data = pageQuery.getData();
		List<TbCodeManageVo> tbCodeManageVos = mapper.pageList(page, data);
		PageVo pageVo = BeanUtil.copyProperties(page, PageVo.class);
		pageVo.setRecords(tbCodeManageVos);
		return pageVo;
	}

	@Override
	public TbCodeManageVo getByCodeId(String codeId) {
		return mapper.getByCodeId(codeId);
	}

	@Override
	public boolean update(TbCodeManageEntity tbCodeManageEntity) {
		LambdaUpdateWrapper<TbCodeManageEntity> wrapper = Wrappers.lambdaUpdate();
		wrapper.eq(TbCodeManageEntity::getCodeId, tbCodeManageEntity.getCodeId());
		return this.update(tbCodeManageEntity, wrapper);
	}

	@Override
	public boolean removes(List<String> codeIds) {
		if (ComUtil.listIsEmpty(codeIds)) return false;
		LambdaQueryWrapper<TbCodeManageEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.in(TbCodeManageEntity::getCodeId, codeIds);
		return this.remove(wrapper);
	}

	@Override
	public List<TbCodeManageVo> selectAll() {
		return this.mapper.selectAll();
	}

	@Override
	public List<TbCodeManageVo> listByCodeMain(String codeMain) {
		if (StrUtil.isEmpty(codeMain)) return null;
		return mapper.listByCodeMain(codeMain);
	}

	@Override
	public List<TbCodeManageEntity> listByCodeMains(List<String> codeMains) {
		if (ComUtil.listIsEmpty(codeMains)) return null;
		return super.list(Wrappers.<TbCodeManageEntity>lambdaQuery().in(TbCodeManageEntity::getCodeMain,codeMains));
	}
}
